using System.Diagnostics.CodeAnalysis;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace Bookvar.UI
{
    public class CloseableTabItem : TabItem
    {
        [SuppressMessage( "Microsoft.Performance", "CA1810:InitializeReferenceTypeStaticFieldsInline" )]
        static CloseableTabItem()
        {
            DefaultStyleKeyProperty.OverrideMetadata( typeof ( CloseableTabItem ),
                                                      new FrameworkPropertyMetadata( typeof ( CloseableTabItem ) ) );
        }

        public static readonly RoutedEvent CloseTabEvent =
            EventManager.RegisterRoutedEvent( "CloseTab", RoutingStrategy.Bubble,
                                              typeof ( RoutedEventHandler ), typeof ( CloseableTabItem ) );

        public event RoutedEventHandler CloseTab
        {
            add { AddHandler( CloseTabEvent, value ); }
            remove { RemoveHandler( CloseTabEvent, value ); }
        }

        public int Index
        {
            get { return (int) GetValue( IndexProperty ); }
            set { SetValue( IndexProperty, value ); }
        }

        public static readonly DependencyProperty IndexProperty =
            DependencyProperty.Register( "Index", typeof ( int ), typeof ( CloseableTabItem ),
                                         new UIPropertyMetadata( 0, OnIndexChanged ) );

        private static void OnIndexChanged( DependencyObject d, DependencyPropertyChangedEventArgs e )
        {
            int index = (int) e.NewValue;
            CloseableTabItem item = (CloseableTabItem) d;

            Brush greenTabBrush =
                item.FindResource( new ComponentResourceKey( typeof ( CloseableTabItem ), "GreenTabBrush" ) ) as
                Brush;
            Brush yellowTabBrush =
                item.FindResource( new ComponentResourceKey( typeof ( CloseableTabItem ), "YellowTabBrush" ) ) as
                Brush;
            Brush blueTabBrush =
                item.FindResource( new ComponentResourceKey( typeof ( CloseableTabItem ), "BlueTabBrush" ) ) as
                Brush;
            Brush purpleTabBrush =
                item.FindResource( new ComponentResourceKey( typeof ( CloseableTabItem ), "PurpleTabBrush" ) ) as
                Brush;

            switch ( index % 4 )
            {
                case 0 :
                {
                    item.Background = greenTabBrush;
                    break;
                }

                case 1 :
                {
                    item.Background = yellowTabBrush;
                    break;
                }

                case 2 :
                {
                    item.Background = blueTabBrush;
                    break;
                }
                case 3 :
                {
                    item.Background = purpleTabBrush;
                    break;
                }
            }
        }


        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            Button closeButton = GetTemplateChild( "PART_Close" ) as Button;
            if ( closeButton != null )
                closeButton.Click += OnCloseButtonClick;
        }

        private void OnCloseButtonClick( object sender, RoutedEventArgs e )
        {
            RaiseEvent( new RoutedEventArgs( CloseTabEvent, this ) );
        }


        //public int Index
        //{

        //    get 
        //    {
        //        CloseableTabControl control = this.Parent as CloseableTabControl;
        //        if (control != null)
        //        {
        //            control.ItemContainerGenerator.IndexFromContainer(this);
        //        }
        //        return 0; 
        //    }


        //}
    }
}